package com.electromagnetic.industry.software.manage.controller;

import cn.hutool.core.util.StrUtil;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread1;
import com.electromagnetic.industry.software.manage.ai.ChatTaskThread2;
import com.electromagnetic.industry.software.manage.ai.ThreadUtil;
import com.electromagnetic.industry.software.manage.pojo.req.QueryDTO;
import com.electromagnetic.industry.software.manage.service.serviceimpl.ChatService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Slf4j
@RestController
@RequestMapping("/data/ed/ai")
public class AiController {

    @Resource
    private ChatService chatService;

    @RequestMapping("/upload")
    public ElectromagneticResult<?> upload(@RequestParam("file") MultipartFile file) throws Exception {
        return chatService.addFromUpload(file);
    }

//    @PostMapping("/chat")
//    public ElectromagneticResult<String> chat(@RequestBody QueryDTO queryDTO) throws Exception {
//        log.info("question is --->" + queryDTO.getMsg());
//        ChatTaskThread<String> chatTaskThread = new ChatTaskThread<>(chatService, queryDTO);
//        Future<String> future = ThreadUtil.getThreadPool().submit(chatTaskThread);
//        String res = future.get();
//        log.info("answer is --->" + res);
//        return ElectromagneticResultUtil.success(res);
//    }

    @PostMapping(path = "/chatStreamStr", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chatStreamStr(@RequestBody QueryDTO queryDTO) throws ExecutionException, InterruptedException {
        if (StrUtil.isEmpty(queryDTO.getMsg())) {
            return Flux.empty();
        }
        queryDTO.setUserId(UserThreadLocal.getUserId());
        ChatTaskThread1 chatTaskThread = new ChatTaskThread1(chatService, queryDTO);
        Future<Flux<String>> future = ThreadUtil.getThreadPool().submit(chatTaskThread);
        return future.get();
    }

    @PostMapping(path = "/chatStreamResp", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> chatStreamResp(@RequestBody QueryDTO queryDTO) throws ExecutionException, InterruptedException {
        if (StrUtil.isEmpty(queryDTO.getMsg())) {
            return Flux.empty();
        }
        queryDTO.setUserId(UserThreadLocal.getUserId());
        ChatTaskThread2 chatTaskThread2 = new ChatTaskThread2(chatService, queryDTO);
        Future<Flux<ChatResponse>> future = ThreadUtil.getThreadPool().submit(chatTaskThread2);
        return future.get();
    }

}
